package org.qf.dao.impl;

import com.mysql.cj.util.StringUtils;
import org.qf.dao.ProviderDao;
import org.qf.pojo.SmbmsProvider;
import org.qf.pojo.SmbmsUser;
import org.qf.utils.BaseDao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @author young
 * @create 2021-01-14-11:36
 * @description
 */
public class ProviderDaoImpl extends BaseDao implements ProviderDao {

    @Override
    public List<SmbmsProvider> queryUserByProName(String proName, int currentPageNo, int pageSize) throws Exception {
        List<SmbmsProvider> list=new ArrayList<SmbmsProvider>();
        List<Object> obj=new ArrayList<Object>();
        StringBuffer sql=new StringBuffer();
        sql.append("select * from smbms_provider ");
        if (!StringUtils.isNullOrEmpty(proName))
        {
            sql.append("where proName like ?");
            obj.add("%"+proName+"%");
        }
        sql.append(" order by creationDate desc limit ?,?");
        obj.add((currentPageNo-1)*pageSize);
        obj.add(pageSize);
        Object[] params=obj.toArray();
        System.out.println(sql.toString()+"========================");
        System.out.println(params+"=====================");
        ResultSet resultSet = executeSQL(sql.toString(), params);
        while (resultSet.next())
        {
            SmbmsProvider smbmsProvider=new SmbmsProvider();
            smbmsProvider.setId(resultSet.getLong("id"));
            smbmsProvider.setProCode(resultSet.getString("proCode"));
            smbmsProvider.setProName(resultSet.getString("proName"));
            smbmsProvider.setProDesc(resultSet.getString("proDesc"));
            smbmsProvider.setProContact(resultSet.getString("proContact"));
            smbmsProvider.setProPhone(resultSet.getString("proPhone"));
            smbmsProvider.setProAddress(resultSet.getString("proAddress"));
            smbmsProvider.setProFax(resultSet.getString("proFax"));
            smbmsProvider.setCreationDate(resultSet.getDate("creationDate"));
            list.add(smbmsProvider);
        }
    return list;
}

    @Override
    public int getSmbmsProviderCount(String proName) throws Exception {
        int count=0;
        StringBuffer sql=new StringBuffer();
        List<Object> obj=new ArrayList<Object>();
        sql.append("select count(*) from smbms_provider ");
        if (!StringUtils.isNullOrEmpty(proName))
        {
            sql.append("where proName like ?");
            obj.add("%"+proName+"%");
        }
        Object[] params=obj.toArray();
        System.out.println(sql+"==========================");
        ResultSet resultSet=executeSQL(sql.toString(),params);
        while(resultSet.next())
        {
            count=resultSet.getInt(1);
        }
        return count;
    }

    @Override
    public SmbmsProvider queryByid(Long id) throws Exception {
        SmbmsProvider smbmsProvider=new SmbmsProvider();
        String sql="select * from smbms_provider where id=?";
        Object[] params={id};
        ResultSet resultSet=executeSQL(sql,params);
        while (resultSet.next())
        {
            smbmsProvider.setId(resultSet.getLong("id"));
            smbmsProvider.setProCode(resultSet.getString("proCode"));
            smbmsProvider.setProName(resultSet.getString("proName"));
            smbmsProvider.setProContact(resultSet.getString("proContact"));
            smbmsProvider.setProPhone(resultSet.getString("proPhone"));
            smbmsProvider.setProFax(resultSet.getString("proFax"));
            smbmsProvider.setProDesc(resultSet.getString("proDesc"));
            smbmsProvider.setProAddress(resultSet.getString("proAddress"));
        }
        return smbmsProvider;
    }

    @Override
    public int updateProvider(SmbmsProvider smbmsProvider) throws Exception {
//        int count=0;
//        String sql="update smbms_user set userName=?,gender=?,birthday=?,phone=?,address=?,userRole=? where id=?";
//        Object[] params= {smbmsUser.getUserName(),smbmsUser.getGender(),smbmsUser.getBirthday(),smbmsUser. getPhone(),smbmsUser.getAddress(),smbmsUser.getUserRole(),smbmsUser.getId()};
//        count= executeUpdate(sql, params);
//        return count;
        int count=0;
        String sql="update smbms_provider set proCode=?,proName=?,proContact=?,proPhone=?,proAddress=?,proFax=?,proDesc=? where id=?";
        Object[] params={smbmsProvider.getProCode(),smbmsProvider.getProName(),smbmsProvider.getProContact(),smbmsProvider.getProPhone(),smbmsProvider.getProAddress(),smbmsProvider.getProFax(),smbmsProvider.getProDesc(),smbmsProvider.getId()};
        count=executeUpdate(sql,params);
        return count;
    }

    @Override
    public int deleteProvider(Long id) throws Exception {
        int count=0;
        String sql="delete from smbms_provider where id=?";
        Object[] params={id};
        count = executeUpdate(sql, params);
        return count;
    }

    @Override
    public int addProvider(SmbmsProvider smbmsProvider) throws Exception {
//        int count=0;
//        String sql="insert into smbms_user(userCode, userName, userPassword, gender, birthday, phone, address, userRole, createdBy, creationDate)values(?,?,?,?,?,?,?,?,?,?)";
//        Object[] params= {smbmsUser.getUserCode(),smbmsUser.getUserName(),smbmsUser.getUserPassword(),smbmsUser.getGender(),smbmsUser.getBirthday(),smbmsUser.getPhone(),smbmsUser.getAddress (),smbmsUser.getUserRole(),smbmsUser.getCreatedBy(),smbmsUser.getCreationDate()};
//        count = executeUpdate(sql, params);
//        return count;
        int count=0;
        String sql="insert into smbms_provider(proCode,proName,proContact,proPhone,proAddress,proFax,proDesc)values(?,?,?,?,?,?,?)";
        Object[] params={smbmsProvider.getProCode(),smbmsProvider.getProName(),smbmsProvider.getProContact(),smbmsProvider.getProPhone(),smbmsProvider.getProAddress(),smbmsProvider.getProFax(),smbmsProvider.getProDesc()};
        count=executeUpdate(sql,params);
        return count;
    }
}

